home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet multimedia / Animacje, filmy i prezentacje / Modelowanie 3D / K-3D 0.6.5.0 / k3d-all-in-one-setup-0.6.5.0.exe / k3d-setup-0.6.5.0.exe / share / shaders / k3d_locillum.h < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-19  |  4.6 KB  |  147 lines

  1. /************************************************************************
  2.  * locillum.h - Functions that compute the light response of locillums.
  3.  *
  4.  * Author: Larry Gritz (gritzl@acm.org)
  5.  *
  6.  * Reference:
  7.  *   _Advanced RenderMan: Creating CGI for Motion Picture_, 
  8.  *   by Anthony A. Apodaca and Larry Gritz, Morgan Kaufmann, 1999.
  9.  *
  10.  * $Revision: 1.1 $    $Date: 2004/05/19 18:15:19 $
  11.  *
  12.  ************************************************************************/
  13.  
  14.  
  15. #ifndef LOCILLUM_H
  16. #define LOCILLUM_H
  17.  
  18.  
  19.  
  20.  
  21. /*
  22.  * Oren and Nayar's generalization of Lambert's reflection model.
  23.  * The roughness parameter gives the standard deviation of angle
  24.  * orientations of the presumed surface grooves.  When roughness=0,
  25.  * the model is identical to Lambertian reflection.
  26.  */
  27. color
  28. LocIllumOrenNayar (normal N;  vector V;  float roughness;)
  29. {
  30.     /* Surface roughness coefficients for Oren/Nayar's formula */
  31.     float sigma2 = roughness * roughness;
  32.     float A = 1 - 0.5 * sigma2 / (sigma2 + 0.33);
  33.     float B = 0.45 * sigma2 / (sigma2 + 0.09);
  34.     /* Useful precomputed quantities */
  35.     float  theta_r = acos (V . N);        /* Angle between V and N */
  36.     vector V_perp_N = normalize(V-N*(V.N)); /* Part of V perpendicular to N */
  37.  
  38.     /* Accumulate incoming radiance from lights in C */
  39.     color  C = 0;
  40.     extern point P;
  41.     illuminance (P, N, PI/2) {
  42.     /* Must declare extern L & Cl because we're in a function */
  43.     extern vector L;  extern color Cl;
  44.     float nondiff = 0;
  45.     lightsource ("__nondiffuse", nondiff);
  46.     if (nondiff < 1) {
  47.         vector LN = normalize(L);
  48.         float cos_theta_i = LN . N;
  49.         float cos_phi_diff = V_perp_N . normalize(LN - N*cos_theta_i);
  50.         float theta_i = acos (cos_theta_i);
  51.         float alpha = max (theta_i, theta_r);
  52.         float beta = min (theta_i, theta_r);
  53.         C += (1-nondiff) * Cl * cos_theta_i * 
  54.         (A + B * max(0,cos_phi_diff) * sin(alpha) * tan(beta));
  55.     }
  56.     }
  57.     return C;
  58.  
  59. }
  60.  
  61.  
  62.  
  63. /*
  64.  * Greg Ward Larson's anisotropic specular local illumination model.
  65.  * The derivation and formulae can be found in:  Ward, Gregory J.
  66.  * "Measuring and Modeling Anisotropic Reflection," ACM Computer 
  67.  * Graphics 26(2) (Proceedings of Siggraph '92), pp. 265-272, July, 1992.
  68.  * Notice that compared to the paper, the implementation below appears
  69.  * to be missing a factor of 1/pi, and to have an extra L.N term.
  70.  * This is not an error!  It is because the paper's formula is for the
  71.  * BRDF, which is only part of the kernel of the light integral, whereas
  72.  * shaders must compute the result of the integral.
  73.  *
  74.  * Inputs:
  75.  *   N - unit surface normal
  76.  *   V - unit viewing direction (from P toward the camera)
  77.  *   xdir - a unit tangent of the surface defining the reference
  78.  *          direction for the anisotropy.
  79.  *   xroughness - the apparent roughness of the surface in xdir.
  80.  *   yroughness - the roughness for the direction of the surface 
  81.  *          tangent perpendicular to xdir.
  82.  */
  83. color
  84. LocIllumWardAnisotropic (normal N;  vector V;
  85.                          vector xdir;  float xroughness, yroughness;)
  86. {
  87.     float sqr (float x) { return x*x; }
  88.  
  89.     float cos_theta_r = clamp (N.V, 0.0001, 1);
  90.     vector X = xdir / xroughness;
  91.     vector Y = (N ^ xdir) / yroughness;
  92.  
  93.     color C = 0;
  94.     extern point P;
  95.     illuminance (P, N, PI/2) {
  96.     /* Must declare extern L & Cl because we're in a function */
  97.     extern vector L;  extern color Cl; 
  98.     float nonspec = 0;
  99.     lightsource ("__nonspecular", nonspec);
  100.     if (nonspec < 1) {
  101.         vector LN = normalize (L);
  102.         float cos_theta_i = LN . N;
  103.         if (cos_theta_i > 0.0) {
  104.         vector H = normalize (V + LN);
  105.         float rho = exp (-2 * (sqr(X.H) + sqr(Y.H)) / (1 + H.N))
  106.             / sqrt (cos_theta_i * cos_theta_r);
  107.         C += Cl * ((1-nonspec) * cos_theta_i * rho);
  108.         }
  109.     }
  110.     }
  111.     return C / (4 * xroughness * yroughness);
  112. }
  113.  
  114.  
  115.  
  116. /*
  117.  * LocIllumGlossy - a possible replacement for specular(), with a
  118.  * more uniformly bright core and a sharper falloff.  It's a nice
  119.  * specular function to use for something made of glass or liquid.
  120.  * Inputs:
  121.  *  roughness - related to the size of the highlight, larger is bigger
  122.  *  sharpness - 1 is infinitely sharp, 0 is very dull
  123.  */
  124. color LocIllumGlossy ( normal N;  vector V;
  125.                        float roughness, sharpness; )
  126. {
  127.     color C = 0;
  128.     float w = .18 * (1-sharpness);
  129.     extern point P;
  130.     illuminance (P, N, PI/2) {
  131.     /* Must declare extern L & Cl because we're in a function */
  132.     extern vector L;  extern color Cl; 
  133.     float nonspec = 0;
  134.     lightsource ("__nonspecular", nonspec);
  135.     if (nonspec < 1) {
  136.         vector H = normalize(normalize(L)+V);
  137.         C += Cl * ((1-nonspec) * 
  138.                smoothstep (.72-w, .72+w,
  139.                    pow(max(0,N.H), 1/roughness)));
  140.     }
  141.     }
  142.     return C;
  143. }
  144.  
  145. #endif /* defined(LOCILLUM_H) */
  146.  
  147.